/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.derivative;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.sql.PreparedStatement;

/**
 * Creates a derivative converter capable of performing a specific type of data type derivation function. For
 * example, the ability to derive a <code>PreparedStatement</code> for a specific message bean, or the ability to
 * derive a <code>byte[]</code> for a specific message bean.
 * <p/>
 * The resulting derivation is used in a specific operation, e.g. storing the message bean in a database or serializing
 * its content into a package file.
 * <p/>
 * The benefit of the derivation factory is that it separates out conversion logic from the message bean and the various
 * persistence service manager interfaces. For example, both the
 * {@link org.proteusframework.platformservice.persistence.api.IMessageBeanManager} and the
 * {@link org.proteusframework.platformservice.persistence.api.IPackageManager} implementations rely on a
 * derivative converter to transform a message bean into a context-specific representation.
 *
 * @author Tacoma Four
 */
public final class DerivativeFactory
{
    private DerivativeFactory()
    {
    }

    /**
     * Instantiates a derivative converter for the appropriate target. For example, to obtain a derivative
     * converter for working with a <code>java.sql.PreparedStatement</code>:
     * <p/>
     * <pre>IDerivativeConverter<PreparedStatement> derivativeConverter
     *              = DerivativeFactory.createDerivativeConverter(PreparedStatement.class);
     * derivativeConverter.convert(pstmt, beanProperty.getDataType(), value, position++);
     *              </pre>
     *
     * @param targetDerivative Type of derivative converter being requested
     * @param <T>              Specifies the type of derivative converter to create
     * @return Derivative converter, or null if the factory cannot handle the targetDerivative class
     */
    public static <T> IDerivativeConverter<T> createDerivativeConverter(Class<T> targetDerivative)
    {
        IDerivativeConverter<T> converter = null;

        if (targetDerivative.equals(PreparedStatement.class))
        {
            converter = (IDerivativeConverter<T>) new JDBCDerivativeConverter();
        } else if (targetDerivative.equals(DataOutputStream.class))
        {
            converter = (IDerivativeConverter<T>) new DataOutputStreamDerivativeConverter();
        } else if (targetDerivative.equals(DataInputStream.class))
        {
            converter = (IDerivativeConverter<T>) new DataInputStreamDerivativeConverter();
        }

        return converter;
    }
}
